---@diagnostic disable: trailing-space
local extension = Package:new("ysch_cards_derived", Package.CardPack)
extension.extensionName = "ysch_Al"

local U = require "packages/utility/utility"
Fk:loadTranslationTable{
    ["ysch_cards_derived"] = "妍设长河衍生牌",
}

--[[

  基本牌

]]--

local slash = Fk:cloneCard("slash")
local shotSlashSkill = fk.CreateActiveSkill{
    name = "ysch_Al_shot__slash_skill",
    max_phase_use_time = 1,
    target_num = 1,
    can_use = slash.skill.canUse,
    mod_target_filter = function(self, to_select, selected, user, card, distance_limited)
      local player = Fk:currentRoom():getPlayerById(to_select)
      local from = Fk:currentRoom():getPlayerById(user.id)
      return from ~= player
    end,
    target_filter = function(self, to_select, selected, _, card, extra_data)
      local count_distances = not (extra_data and extra_data.bypass_distances)
      if #selected < self:getMaxTargetNum(Self, card) then
        local player = Fk:currentRoom():getPlayerById(to_select)
        return self:modTargetFilter(to_select, selected, Self, card, count_distances) and
        (
          #selected > 0 or
          Self.phase ~= Player.Play or
          (extra_data and extra_data.bypass_times) or
          self:withinTimesLimit(Self, Player.HistoryPhase, card, "slash", player)
        )
      end
    end,
    on_effect = function(self, room, effect)
      local to = effect.to
      local from = effect.from
      room:damage({
        from = room:getPlayerById(from),
        to = room:getPlayerById(to),
        card = effect.card,
        damage = 1,
        damageType = fk.NormalDamage,
        skillName = self.name
      })
    end
}
local shotSlash = fk.CreateBasicCard{
    name = "&ysch_Al_shot__slash",
    skill = shotSlashSkill,
    is_damage_card = true,
    suit = Card.Heart,
    number = 13,
}
extension:addCard(shotSlash)
Fk:loadTranslationTable{
    ["ysch_Al_shot__slash_skill"] = "射杀",
    ["ysch_Al_shot__slash"] = "射杀",
    [":ysch_Al_shot__slash"] = "基本牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一名其他角色<br /><b>效果</b>：对目标角色造成1点伤害。<br/>",
}

local shashanSkill = fk.CreateActiveSkill{
    name = "ysch_Al_shashan_skill",
    can_use = Util.FalseFunc,
}
local shashanUse = fk.CreateViewAsSkill{
    name = "ysch_Al_shashan_use&",
    mute = true,
    global = true,
    anim_type = "special",
    pattern = "slash,jink",
    card_num = 1,
    interaction = function()
        local names = {}
        if Fk.currentResponsePattern == nil and Self:canUse(Fk:cloneCard("slash")) then
            table.insertIfNeed(names, "slash")
        else
            for _, name in ipairs({"slash", "jink"}) do
            if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(Fk:cloneCard(name)) then
                table.insertIfNeed(names, name)
            end
            end
        end
        if #names == 0 then return end
        return UI.ComboBox {choices = names}
    end,
    card_filter = function(self, to_select, selected)
        return #selected == 0 and Fk:getCardById(to_select).name == "shashan"
    end,
    view_as = function(self, cards)
        if #cards ~= 1 or self.interaction.data == nil then return end
        local card = Fk:cloneCard(self.interaction.data)
        card.skillName = self.name
        card:addSubcard(cards[1])
        return card
    end,
    enabled_at_play = function(self, player)
        return table.find(player:getHandlyIds(true), function (id)
            return Fk:getCardById(id).name == "shashan"
        end)
    end,
    enabled_at_response = function(self, player)
        return table.find(player:getHandlyIds(true), function (id)
            return Fk:getCardById(id).name == "shashan"
        end)
    end,
}
local shade = fk.CreateBasicCard{
    name = "&shashan",
    skill = shashanSkill,
    suit = Card.Heart,
    number = 12,
}
Fk:addSkill(shashanUse)
extension:addCard(shade)
Fk:loadTranslationTable{
    ["shashan"] = "杀闪",
    [":shashan"] = "基本牌<br/><b>效果</b>：此牌可当【杀】或【闪】使用或打出。",
    ["ysch_Al_shashan_skill"] = "杀/闪",
    ["ysch_Al_shashan_use&"] = "杀/闪",
    [":ysch_Al_shashan_use&"] = "将【杀/闪】当【杀】或【闪】使用或打出。",
}

--[[

  锦囊牌

]]--

local jintuiSkill = fk.CreateActiveSkill{
    name = "ysch_Al__jintui_skill",
    can_use = Util.GlobalCanUse,
    on_use = Util.GlobalOnUse,
    mod_target_filter = Util.TrueFunc,
    on_action = function(self, room, use, finished)
        if not finished then
            local player = room:getPlayerById(use.from)
            local choice = room:askForChoice(player, { "#ysch_Al__jintui-choice1", "#ysch_Al__jintui-choice2" }, self.name)
            use.extra_data = use.extra_data or {}
            use.extra_data.interaction = choice
        end

        if use.extra_data.interaction ~= "#ysch_Al__jintui-choice1" then
            if not finished then
                local toDisplay = room:getNCards(#TargetGroup:getRealTargets(use.tos))
                room:moveCards({
                    ids = toDisplay,
                    toArea = Card.Processing,
                    moveReason = fk.ReasonPut,
                    proposer = use.from,
                })
            
                table.forEach(room.players, function(p)
                    room:fillAG(p, toDisplay)
                end)
            
                use.extra_data = use.extra_data or {}
                use.extra_data.AGFilled = toDisplay
                use.extra_data.AGResult = {}
            else
                if use.extra_data and use.extra_data.AGFilled then
                    table.forEach(room.players, function(p)
                    room:closeAG(p)
                    end)
            
                    local toDiscard = table.filter(use.extra_data.AGFilled, function(id)
                    return room:getCardArea(id) == Card.Processing
                    end)
            
                    if #toDiscard > 0 then
                    room:moveCards({
                        ids = toDiscard,
                        toArea = Card.DiscardPile,
                        moveReason = fk.ReasonPutIntoDiscardPile,
                    })
                    end
                end
            
                use.extra_data.AGFilled = nil
            end
        else
            for i, v in ipairs(use.tos) do
                if v[1] == use.from then
                    table.remove(use.tos,i)
                end
            end
        end
    end,
    on_effect = function(self, room, effect)
        if effect.extra_data.interaction == "#ysch_Al__jintui-choice2" then
            local to = room:getPlayerById(effect.to)
            if not (effect.extra_data and effect.extra_data.AGFilled) then
            return
            end
        
            local chosen = room:askForAG(to, effect.extra_data.AGFilled, false, self.name)
            room:takeAG(to, chosen, room.players)
            table.insert(effect.extra_data.AGResult, {effect.to, chosen})
            room:moveCardTo(chosen, Card.PlayerHand, effect.to, fk.ReasonPrey, self.name, nil, true, effect.to)
            table.removeOne(effect.extra_data.AGFilled, chosen)
        else
            local cardResponded = room:askForResponse(room:getPlayerById(effect.to), 'slash', nil, nil, true, nil, effect)

            if cardResponded then
            room:responseCard({
                from = effect.to,
                card = cardResponded,
                responseToEvent = effect,
            })
            else
            room:damage({
                from = room:getPlayerById(effect.from),
                to = room:getPlayerById(effect.to),
                card = effect.card,
                damage = 1,
                damageType = fk.NormalDamage,
                skillName = self.name,
            })
            end
        end
    end

}
local jintui = fk.CreateTrickCard{
    name = "&jintuiziru",
    suit = Card.Spade,
    number = 7,
    multiple_targets = true,
    skill = jintuiSkill,
}
extension:addCard(jintui)
Fk:loadTranslationTable{
    ["jintuiziru"] = "进退自如",
    [":jintuiziru"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：所有角色<br/><b>效果</b>：选择一项：1.所有其他角色需打出一张【杀】，否则受到你的1点伤害；2.亮出牌堆顶等于存活角色数的牌，所有角色各获得其中一张牌。",
    ["ysch_Al__jintui_skill"] = "进退自如",
    ["#ysch_Al__jintui-choice1"] = "其他角色出【杀】",
    ["#ysch_Al__jintui-choice2"] = "所有角色选择拿牌",
}

local enemyAtTheGatesSkill = fk.CreateActiveSkill{
    name = "ysch_Al__enemy_at_the_gates_skill",
    prompt = "#ysch_Al__enemy_at_the_gates_skill",
    can_use = Util.CanUse,
    target_num = 1,
    mod_target_filter = function(self, to_select, selected, user, card)
        return user.id ~= to_select
    end,
    target_filter = Util.TargetFilter,
    on_effect = function(self, room, cardEffectEvent)
        local player = room:getPlayerById(cardEffectEvent.from)
        local to = room:getPlayerById(cardEffectEvent.to)
        local cards = room:getNCards(4)
        local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
        if use_event then
            local use = use_event.data[1]
            use.extra_data = use.extra_data or {}
            use.extra_data.showCard = use.extra_data.showCard or {}
            table.insertIfNeed(use.extra_data.showCard, cards)
        end
        room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, self.name, nil, true, player.id)
        for _, id in ipairs(cards) do
            local card = Fk:getCardById(id)
            if card.trueName == "slash" and not player:prohibitUse(card) and not player:isProhibited(to, card) and to:isAlive() then
                if use_event then
                    local use = use_event.data[1]
                    use.extra_data = use.extra_data or {}
                    use.extra_data.useSha = use.extra_data.useSha or {}
                    table.insertIfNeed(use.extra_data.useSha, id)
                end
                card.skillName = self.name
                room:useCard({
                    card = card,
                    from = player.id,
                    tos = { {to.id} },
                    extraUse = true,
                })
            end
        end
        cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
        if #cards > 0 then
            room:delay(#cards * 150)
            room:moveCardTo(table.reverse(cards), Card.DrawPile, nil, fk.ReasonPut, self.name, nil, true, player.id)
        end
    end,
}
local enemyAtTheGates = fk.CreateTrickCard{
    name = "&ysch_Al__enemy_at_the_gates",
    suit = Card.Spade,
    number = 7,
    skill = enemyAtTheGatesSkill,
}
extension:addCards{
    enemyAtTheGates,
}
Fk:loadTranslationTable{
    ["ysch_Al__enemy_at_the_gates"] = "兵临城下",
    [":ysch_Al__enemy_at_the_gates"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：一名其他角色<br /><b>效果</b>：你展示牌堆顶的四张牌，依次对目标角色使用其中的【杀】，然后将其余的牌以原顺序放回牌堆顶。",
    ["#ysch_Al__enemy_at_the_gates_skill"] = "选择一名其他角色，你展示牌堆顶四张牌，依次对其使用其中【杀】，其余牌放回牌堆顶",
    ["ysch_Al__enemy_at_the_gates_skill"] = "兵临城下",
}

local adaptationSkill = fk.CreateActiveSkill{
    name = "ysch_Al__adaptation_skill",
    can_use = Util.FalseFunc
}
local adaptationTriggerSkill = fk.CreateTriggerSkill{
    name = "ysch_Al__adaptation_trigger_skill",
    global = true,
  
    refresh_events = {fk.PreCardUse, fk.PreCardRespond},
    can_refresh = function(self, event, target, player, data)
      return target == player and (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
    end,
    on_refresh = function(self, event, target, player, data)
      if string.find(data.card.name, "adaptation") then return end
      player.room:setPlayerMark(player, "ysch_Al__adaptation-turn", data.card.name)
    end,
}
local adaptationFilterSkill = fk.CreateFilterSkill{
    name = "ysch_Al__adaptation_filter_skill",
    mute = true,
    global = true,
    card_filter = function(self, card, player)
      return card.name == "ysch_Al__adaptation" and player:getMark("ysch_Al__adaptation-turn") ~= 0
    end,
    view_as = function(self, card, player)
      return Fk:cloneCard(player:getMark("ysch_Al__adaptation-turn"), card.suit, card.number)
    end,
}
local adaptation = fk.CreateTrickCard{
    name = "&ysch_Al__adaptation",
    skill = adaptationSkill,
    suit = Card.Spade,
    number = 10,
}
Fk:addSkill(adaptationTriggerSkill)
Fk:addSkill(adaptationFilterSkill)
adaptation.package = extension
Fk:addCard(adaptation)
Fk:loadTranslationTable{
    ["ysch_Al__adaptation"] = "随机应变",
    ["ysch_Al__adaptation_filter_skill"] = "随机应变",
    [":ysch_Al__adaptation"] = "锦囊牌<br/><b>效果</b>：此牌视为你本回合使用或打出的上一张基本牌或普通锦囊牌（随机应变除外）。",
}

local UnderhandingSkill = fk.CreateActiveSkill{
    name = "ysch_Al__underhanding_skill",
    prompt = "#ysch_Al__underhanding_skill",
    can_use = Util.CanUse,
    min_target_num = 1,
    max_target_num = 2,
    mod_target_filter = function(self, to_select, selected, player, card)
        return to_select ~= player.id and not Fk:currentRoom():getPlayerById(to_select):isAllNude()
    end,
    target_filter = Util.TargetFilter,
    on_effect = function(self, room, effect)
      local player = room:getPlayerById(effect.from)
      local to = room:getPlayerById(effect.to)
        if not to:isAllNude() then
            local id = room:askForCardChosen(player, to, "hej", self.name)
            room:obtainCard(player, id, false, fk.ReasonPrey, player.id, self.name)
            local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
            if e then
                local use = e.data[1]
                use.extra_data = use.extra_data or {}
                use.extra_data.underhanding_targets = use.extra_data.underhanding_targets or {}
                use.extra_data.underhanding_gain = use.extra_data.underhanding_gain or {}
                table.insertIfNeed(use.extra_data.underhanding_targets, to.id)
                table.insertIfNeed(use.extra_data.underhanding_gain, id)
            end
        end
    end,
    on_action = function (self, room, use, finished)
        if not finished then return end
        local player = room:getPlayerById(use.from)
        if player.dead or player:isNude() then return end
        local targets = (use.extra_data or {}).underhanding_targets or {}
        if #targets == 0 then return end
        room:sortPlayersByAction(targets)
        for _, pid in ipairs(targets) do
            local target = room:getPlayerById(pid)
            if not player:isNude() and not target.dead and not player.dead then
                local c = room:askForCard(player, 1, 1, true, self.name, false, nil, "#ysch_Al__underhanding-card::" .. pid)[1]
                room:moveCardTo(c, Player.Hand, target, fk.ReasonGive, self.name, nil, false, player.id)
                use.extra_data.underhanding_lose = use.extra_data.underhanding_lose or {}
                table.insertIfNeed(use.extra_data.underhanding_lose, c)
            end
        end
    end
}
local ysch_Al__underhandingExclude = fk.CreateMaxCardsSkill{
    name = "ysch_Al__underhanding_exclude",
    global = true,
    exclude_from = function(self, player, card)
        return card and card.name == "ysch_Al__underhanding"
    end,
}
Fk:addSkill(ysch_Al__underhandingExclude)
local Underhanding = fk.CreateTrickCard{
    name = "&ysch_Al__underhanding",
    suit = Card.Heart,
    number = 5,
    skill = UnderhandingSkill,
    multiple_targets = true,
}
extension:addCard(Underhanding)
Fk:loadTranslationTable{
    ["ysch_Al__underhanding"] = "瞒天过海",
    [":ysch_Al__underhanding"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一至两名区域内有牌的其他角色。<br/>"..
    "<b>效果</b>：你依次获得目标角色区域内的一张牌，然后依次交给目标角色一张牌。<br/>此牌不计入你的手牌上限。",
    ["ysch_Al__underhanding_skill"] = "瞒天过海",
    ["ysch_Al__underhanding_action"] = "瞒天过海",
    ["#ysch_Al__underhanding-card"] = "瞒天过海：交给 %dest 一张牌",
    ["#ysch_Al__underhanding_skill"] = "选择一至两名区域内有牌的其他角色，依次获得其区域内的一张牌，然后依次交给其一张牌",
}

local bogusFlowerSkill = fk.CreateActiveSkill{
    name = "ysch_Al__bogus_flower_skill",
    mod_target_filter = Util.TrueFunc,
    on_use = function(self, room, cardUseEvent)
      if not cardUseEvent.tos or #TargetGroup:getRealTargets(cardUseEvent.tos) == 0 then
        cardUseEvent.tos = { { cardUseEvent.from } }
      end
    end,
    on_effect = function(self, room, effect)
      local target = room:getPlayerById(effect.to)
      if target.dead or target:isNude() then return end
      local cards = room:askForDiscard(target, 1, 2, true, "ysch_Al__bogus_flower", false, ".", "#ysch_Al__bogus_flower-discard", true)
      if #cards == 0 then return end
      local n = #cards
      if table.find(cards, function(id) return Fk:getCardById(id).type == Card.TypeEquip end) then
        n = n + 1
      end
      room:throwCard(cards, "ysch_Al__bogus_flower", target, target)
      if not target.dead then
        target:drawCards(n, "ysch_Al__bogus_flower")
      end
    end
}
local ysch_Al__bogus_flower = fk.CreateTrickCard{
    name = "&ysch_Al__bogus_flower",
    skill = bogusFlowerSkill,
}
ysch_Al__bogus_flower.package = extension
Fk:addCard(ysch_Al__bogus_flower)
Fk:loadTranslationTable{
    ["ysch_Al__bogus_flower"] = "树上开花",
    ["ysch_Al__bogus_flower_skill"] = "树上开花",
    [":ysch_Al__bogus_flower"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：你<br/><b>效果</b>：目标角色弃置至多两张牌，然后摸等量的牌；"..
    "若弃置了装备牌，则多摸一张牌。",
    ["#ysch_Al__bogus_flower-discard"] = "树上开花：弃置一至两张牌，摸等量的牌，若弃置了装备牌则多摸一张",
}

local ysch_Al__foresightSkill = fk.CreateActiveSkill{
    name = "ysch_Al__foresight_skill",
    mod_target_filter = Util.TrueFunc,
    can_use = function(self, player, card)
      return Util.SelfCanUse(self, player, card)
    end,
    on_use = function(self, room, cardUseEvent)
      if not cardUseEvent.tos or #TargetGroup:getRealTargets(cardUseEvent.tos) == 0 then
        cardUseEvent.tos = {{cardUseEvent.from}}
      end
    end,
    on_effect = function(self, room, effect)
      local to = room:getPlayerById(effect.to)
      room:askForGuanxing(to, room:getNCards(2), nil, nil)
      room:drawCards(to, 2, self.name)
    end
}
local ysch_Al__foresight = fk.CreateTrickCard{
    name = "&ysch_Al__foresight",
    skill = ysch_Al__foresightSkill,
}
ysch_Al__foresight.package = extension
Fk:addCard(ysch_Al__foresight)
Fk:loadTranslationTable{
    ["ysch_Al__foresight"] = "洞烛先机",
    ["ysch_Al__foresight_skill"] = "洞烛先机",
    [":ysch_Al__foresight"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：你<br/><b>效果</b>：目标角色卜算2（观看牌堆顶的两张牌，"..
    "将其中任意张以任意顺序置于牌堆顶，其余以任意顺序置于牌堆底），然后摸两张牌。",
}

local ysch_Al__drowningSkill = fk.CreateActiveSkill{
    name = "ysch_Al__drowning_skill",
    can_use = Util.CanUse,
    target_num = 1,
    mod_target_filter = function(self, to_select, selected, player, card, distance_limited)
      return to_select ~= player.id
    end,
    target_filter = Util.TargetFilter,
    on_effect = function(self, room, effect)
        local from = room:getPlayerById(effect.from)
        local to = room:getPlayerById(effect.to)
        if #to.player_cards[Player.Equip] == 0 then
            room:damage({
            from = from,
            to = to,
            card = effect.card,
            damage = 1,
            damageType = fk.ThunderDamage,
            skillName = self.name
            })
        else
            if room:askForSkillInvoke(to, self.name, nil, "#ysch_Al__drowning-discard::"..from.id) then
            to:throwAllCards("e")
            else
            room:damage({
                from = from,
                to = to,
                card = effect.card,
                damage = 1,
                damageType = fk.ThunderDamage,
                skillName = self.name
            })
            end
        end
    end
}
local ysch_Al__drowning = fk.CreateTrickCard{
    name = "&ysch_Al__drowning",
    skill = ysch_Al__drowningSkill,
    is_damage_card = true,
}
ysch_Al__drowning.package = extension
Fk:addCard(ysch_Al__drowning)
Fk:loadTranslationTable{
    ["ysch_Al__drowning"] = "水淹七军",
    ["ysch_Al__drowning_skill"] = "水淹七军",
    [":ysch_Al__drowning"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一名其他角色<br /><b>效果</b>：目标角色选择一项："..
    "1.弃置装备区所有牌（至少一张）；2.你对其造成1点雷电伤害。",
    ["#ysch_Al__drowning-discard"] = "水淹七军：“确定”弃置装备区所有牌，或点“取消” %dest 对你造成1点雷电伤害",
}

local wdStopThirstSkill = fk.CreateActiveSkill{
    name = "ysch_Al__wd_stop_thirst_skill",
    target_num = 1,
    prompt = "#ysch_Al__wd_stop_thirst_skill",
    mod_target_filter = Util.TrueFunc,
    target_filter = Util.TargetFilter,
    on_effect = function(self, room, effect)
      local player = room:getPlayerById(effect.from)
      local target = room:getPlayerById(effect.to)
      if target.dead then return end
      if table.every(room.alive_players, function(p) return p:getHandcardNum() >= target:getHandcardNum() end) then
        target:drawCards(2, self.name)
      end
      if not target.dead and target:isWounded() and table.every(room.alive_players, function(p) return p.hp >= target.hp end) then
        room:recover({
          who = target,
          num = 1,
          card = effect.card,
          recoverBy = player,
          skillName = self.name
        })
      end
    end,
}
local ysch_Al__wd_stop_thirst = fk.CreateTrickCard{
    name = "&ysch_Al__wd_stop_thirst",
    skill = wdStopThirstSkill,
}
ysch_Al__wd_stop_thirst.package = extension
Fk:addCard(ysch_Al__wd_stop_thirst)
Fk:loadTranslationTable{
    ["ysch_Al__wd_stop_thirst"] = "望梅止渴",
    ["ysch_Al__wd_stop_thirst_skill"] = "望梅止渴",
    [":ysch_Al__wd_stop_thirst"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一名角色<br/><b>效果</b>：若目标是手牌最少的角色，其摸两张牌；"..
    "然后若目标是体力值最小的角色，其回复1点体力。",
    ["#ysch_Al__wd_stop_thirst_skill"] = "选择一名角色，若其手牌/体力最少，其回1血/摸2牌",
}

local ysch_Al__unexpectationSkill = fk.CreateActiveSkill{
    name = "ysch_Al__unexpectation_skill",
    can_use = Util.CanUse,
    target_num = 1,
    mod_target_filter = function(self, to_select, selected, player, card, distance_limited)
      return to_select ~= player.id and not Fk:currentRoom():getPlayerById(to_select):isKongcheng()
    end,
    target_filter = Util.TargetFilter,
    on_effect = function(self, room, effect)
        local player = room:getPlayerById(effect.from)
        local target = room:getPlayerById(effect.to)
        if target:isKongcheng() then return end
        local card = room:askForCardChosen(player, target, "h", self.name)
        target:showCards(card)
        card = Fk:getCardById(card)
        if target.dead or card.suit == Card.NoSuit or effect.card.suit == Card.NoSuit then return end
        if card.suit ~= effect.card.suit then
            room:damage({
            from = player,
            to = target,
            card = effect.card,
            damage = 1,
            skillName = self.name
            })
        end
    end,
}
local ysch_Al__unexpectation = fk.CreateTrickCard{
    name = "&ysch_Al__unexpectation",
    skill = ysch_Al__unexpectationSkill,
    is_damage_card = true,
}
ysch_Al__unexpectation.package = extension
Fk:addCard(ysch_Al__unexpectation)
Fk:loadTranslationTable{
    ["ysch_Al__unexpectation"] = "出其不意",
    ["ysch_Al__unexpectation_skill"] = "出其不意",
    [":ysch_Al__unexpectation"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一名有手牌的其他角色<br/><b>效果</b>：你展示目标角色的一张手牌，"..
    "若该牌与此【出其不意】花色不同，你对其造成1点伤害。",
}

local snatch = Fk:cloneCard("snatch")
local sincereTreatSkill = fk.CreateActiveSkill{
  name = "ysch_Al__sincere_treat_skill",
  distance_limit = 1,
  target_num = 1,
  mod_target_filter = snatch.skill.modTargetFilter,
  target_filter = snatch.skill.targetFilter,
  on_effect = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.to)
    if player.dead or target.dead or target:isAllNude() then return end
    local cards = room:askForCardsChosen(player, target, 1, 2, "hej", self.name)
    room:obtainCard(player, cards, false, fk.ReasonPrey)
    if not player.dead and not target.dead or player:isKongcheng() then
      local n = math.min(#cards, player:getHandcardNum())
      cards = room:askForCard(player, n, n, false, self.name, false, ".|.|.|hand", "#ysch_Al__sincere_treat-give::"..target.id..":"..n)
      room:obtainCard(target, cards, false, fk.ReasonGive)
    end
  end
}
local ysch_Al__sincere_treat = fk.CreateTrickCard{
  name = "&ysch_Al__sincere_treat",
  skill = sincereTreatSkill,
}
ysch_Al__sincere_treat.package = extension
Fk:addCard(ysch_Al__sincere_treat)
Fk:loadTranslationTable{
  ["ysch_Al__sincere_treat"] = "推心置腹",
  ["ysch_Al__sincere_treat_skill"] = "推心置腹",
  [":ysch_Al__sincere_treat"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：距离为1的一名区域内有牌的其他角色<br/><b>效果</b>：你获得目标角色"..
  "区域里至多两张牌，然后交给其等量的手牌。",
  ["#ysch_Al__sincere_treat-give"] = "推心置腹：请交给 %dest %arg张手牌",
}

--[[

  装备牌

]]--
local breastplateSkill = fk.CreateTriggerSkill{
    name = "#ysch_Al__breastplate_skill",
    attached_equip = "ysch_Al__breastplate",
    events = {fk.DamageInflicted},
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(self) and data.damage >= player.hp
        -- 防一手虚拟装备
        and table.find(player:getEquipments(Card.SubtypeArmor), function(id) return Fk:getCardById(id).name == "ysch_Al__breastplate" end)
    end,
    on_cost = function(self, event, target, player, data)
        return player.room:askForSkillInvoke(player, self.name, data, "#ysch_Al__breastplate-ask:::" .. data.damage .. ":" .. Fk:getDamageNatureName(data.damageType))
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:notifySkillInvoked(player, self.name, "defensive")
        room:sendLog{
            type = "#BreastplateSkill",
            from = player.id,
            arg = self.attached_equip,
            arg2 = data.damage,
            arg3 = Fk:getDamageNatureName(data.damageType),
        }
        room:moveCardTo(table.filter(player:getEquipments(Card.SubtypeArmor), function(id) return Fk:getCardById(id).name == "ysch_Al__breastplate" end),
            Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name, nil, true, player.id)
        return true
    end,
}
Fk:addSkill(breastplateSkill)
local breastplate = fk.CreateArmor{
    name = "&ysch_Al__breastplate",
    suit = Card.Club,
    number = 1,
    equip_skill = breastplateSkill,
}
breastplate.package = extension
Fk:addCard(breastplate)
  
Fk:loadTranslationTable{
    ["breastplate"] = "护心镜",
    ["ysch_Al__breastplate"] = "护心镜",
    ["#ysch_Al__breastplate_skill"] = "护心镜",
    [":ysch_Al__breastplate"] = "装备牌·防具<br/><b>防具技能</b>：当你受到伤害时，若此伤害大于或等于你当前的体力值，你可将装备区里的【护心镜】置入弃牌堆，然后防止此伤害。",
    ["#ysch_Al__breastplate-ask"] = "护心镜：你可将装备区里的【护心镜】置入弃牌堆，防止 %arg 点 %arg2 伤害",
    ["#BreastplateSkill"] = "%from 发动了〖%arg〗，防止了 %arg2 点 %arg3 伤害",
}

local crossbowAudio = fk.CreateTriggerSkill{
    name = "#ysch_Al__crossbowAudio",
    refresh_events = {fk.CardUsing},
    can_refresh = function(self, event, target, player, data)
      return target == player and player:hasSkill(self) and player.phase == Player.Play and
        data.card.trueName == "slash" and player:usedCardTimes("slash", Player.HistoryPhase) > 1
    end,
    on_refresh = function(self, event, target, player, data)
      local room = player.room
      room:broadcastPlaySound("./packages/mobile/audio/card/ysch_Al__crossbow")
      room:setEmotion(player, "./packages/standard_cards/image/anim/crossbow")
    end,
}
local crossbowSkill = fk.CreateTargetModSkill{
    name = "#ysch_Al__crossbow_skill",
    attached_equip = "ysch_Al__crossbow",
    bypass_times = function(self, player, skill, scope)
      if player:hasSkill(self) and skill.trueName == "slash_skill"
        and scope == Player.HistoryPhase then
        return true
      end
    end,
}
crossbowSkill:addRelatedSkill(crossbowAudio)
Fk:addSkill(crossbowSkill)
  
local crossbow = fk.CreateWeapon{
    name = "&ysch_Al__crossbow",
    suit = Card.Club,
    number = 1,
    attack_range = 3,
    equip_skill = crossbowSkill,
}
crossbow.package = extension
Fk:addCard(crossbow)
Fk:loadTranslationTable{
    ["ysch_Al__crossbow"] = "元戎精械弩",
    [":ysch_Al__crossbow"] = "装备牌·武器<br /><b>攻击范围</b>：3<br /><b>武器技能</b>：锁定技。你于出牌阶段内使用【杀】无次数限制。",
}

local ysch_Al__poisonousDaggerSkill = fk.CreateTriggerSkill{
    name = "#ysch_Al__poisonous_dagger_skill",
    attached_equip = "ysch_Al__poisonous_dagger",
    events = {fk.TargetSpecified},
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(self) and data.card and data.card.trueName == "slash" and
        not player.room:getPlayerById(data.to).dead
    end,
    on_cost = function(self, event, target, player, data)
      if player.room:askForSkillInvoke(player, self.name, nil,
        "#ysch_Al__poisonous_dagger-invoke::"..data.to..":"..math.min(player:usedSkillTimes(self.name, Player.HistoryTurn) + 1, 5)) then
        self.cost_data = { tos = { data.to } }
        return true
      end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      room:loseHp(room:getPlayerById(self.cost_data.tos[1]), math.min(player:usedSkillTimes(self.name, Player.HistoryTurn), 5), self.name)
    end,
}
Fk:addSkill(ysch_Al__poisonousDaggerSkill)
local ysch_Al__poisonousDagger = fk.CreateWeapon{
    name = "&ysch_Al__poisonous_dagger",
    suit = Card.Spade,
    number = 1,
    attack_range = 1,
    equip_skill = ysch_Al__poisonousDaggerSkill,
    on_uninstall = function(self, room, player)
      Weapon.onUninstall(self, room, player)
      player:setSkillUseHistory(self.equip_skill.name, 0, Player.HistoryTurn)
    end,
}
ysch_Al__poisonousDagger.package = extension
Fk:addCard(ysch_Al__poisonousDagger)
Fk:loadTranslationTable{
    ["ysch_Al__poisonous_dagger"] = "混毒弯匕",
    ["#ysch_Al__poisonous_dagger_skill"] = "混毒弯匕",
    [":ysch_Al__poisonous_dagger"] = "装备牌·武器<br /><b>攻击范围</b>：1<br /><b>武器技能</b>：当你使用【杀】指定目标后，你可以令目标角色失去X点体力"..
    "（X为此武器本回合发动技能次数且至多为5）。",
    ["#ysch_Al__poisonous_dagger-invoke"] = "混毒弯匕：你可以令 %dest 失去%arg点体力",
}

--[[

  特殊牌

]]--
local shadeSkill = fk.CreateActiveSkill{
  name = "ysch_Al__shade_skill",
  can_use = Util.FalseFunc,
}
local playerH = fk.CreateBasicCard{
    name = "&ysch_Al_h",
    skill = shadeSkill,
}
local playerE = fk.CreateBasicCard{
    name = "&ysch_Al_e",
    skill = shadeSkill,
}
local playerJ = fk.CreateBasicCard{
    name = "&ysch_Al_j",
    skill = shadeSkill,
}
local premeditate = fk.CreateDelayedTrickCard{  --祭祀智慧之不想带江山如故还引用蓄谋
    name = "&ysch_Al__premeditate",
}
playerH.package = extension
playerE.package = extension
playerJ.package = extension
premeditate.package = extension
Fk:addCard(playerH)
Fk:addCard(playerE)
Fk:addCard(playerJ)
Fk:addCard(premeditate)
Fk:loadTranslationTable{
    ["ysch_Al_h"] = "手牌区",
    [":ysch_Al_h"] = "",
    ["ysch_Al_e"] = "装备区",
    [":ysch_Al_e"] = "",
    ["ysch_Al_j"] = "判定区",
    [":ysch_Al_j"] = "",
    ["premeditate"] = "蓄谋",
    ["ysch_Al__premeditate"] = "蓄谋",
    [":ysch_Al__premeditate"] = "这张牌视为延时锦囊<br/><b>效果：</b>判定阶段开始时，按置入顺序（后置入的先处理）依次处理“蓄谋”牌：1.使用此牌，"..
    "然后此阶段不能再使用此牌名的牌；2.将所有“蓄谋”牌置入弃牌堆。",
}

local names = {}
for name, _ in pairs(Fk.all_card_types) do
  table.insertIfNeed(names, name)
end
for _, card in ipairs(extension.cards) do
  table.insertIfNeed(names, card.name)
end

for _, name in ipairs(names) do
    local nameZh = Fk:translate(name, "zh_CN")
    local nameAbbr = nameZh
    local len = nameAbbr:len()
    if len == 1 then
        nameAbbr = nameAbbr
    elseif len > 2 then
        nameAbbr = string.sub(nameAbbr, 1, 6)
    end
    Fk:loadTranslationTable{
        ["@@ysch5_bawang_"..name.."-inhand"] = "霸王"..nameAbbr,
    }
end
return extension